package com.scau.service.impl;

import com.scau.dao.SystemUserMapper;
import com.scau.dto.CBECReturn;
import com.scau.pojo.SystemUser;
import com.scau.pojo.SystemUserExample;
import com.scau.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemUserServiceImpl implements ISystemUserService {
    @Autowired
    SystemUserMapper systemUserMapper;


    @Override
    public List<SystemUser> getUsers() {
        SystemUserExample systemUserExample = new SystemUserExample();
        systemUserExample.createCriteria().andUsernameIsNotNull();
        List<SystemUser> orderList = systemUserMapper.selectByExample(systemUserExample);
        return orderList;
    }

    @Override
    public CBECReturn addUser(SystemUser systemUser) {
        // 校验传入的注册用户数据
        /*if (null == systemUser.getUsername() || systemUser.getUsername().trim().length() <= 0)
            return CBECReturn.build(400, "用户名错误！");
        if (null == systemUser.getEmail() || systemUser.getEmail().trim().length() <= 0)
            return CBECReturn.build(400, "注册邮箱有误！");
        if (null == systemUser.getPhone() || systemUser.getPhone().trim().length() <= 0)
            return CBECReturn.build(400, "注册手机号有误！");
        if (null == systemUser.getType() || systemUser.getType().trim().length() <= 0 ||
                (!"1".equals(systemUser.getType()) && !"2".equals(systemUser.getType()))
            )
            return CBECReturn.build(400, "注册账号类型有误！只能为借卖方或品牌商");
        if (null == systemUser.getPassword() || systemUser.getPassword().trim().length() <= 0)
            return CBECReturn.build(400, "注册密码有误！");
        if (null == systemUser.getMenu() || systemUser.getMenu().trim().length() <= 0)
            return CBECReturn.build(400, "注册账号的菜单类型有误！");*/

        // 插入数据库
        systemUserMapper.insert(systemUser);
        return CBECReturn.ok();
    }

    @Override
    public void updateUser(SystemUser systemUser) {
        systemUserMapper.updateByPrimaryKeySelective(systemUser);
    }

    @Override
    public SystemUser selectUser(String username) {
        SystemUser systemUser = systemUserMapper.selectByPrimaryKey(username);
        return systemUser;
    }

    @Override
    public void deleteUser(String username) {
        systemUserMapper.deleteByPrimaryKey(username);
    }
}
